squid正向代理

        centos 系统自带 squid 包:

1
[root@192 ~]# yum install -y squid

        当然也可以源码包编译安装,Squid官方网站,以下为 squid 编译参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
./configure --prefix=/usr/local/squid \
--disable-dependency-tracking \
--enable-dlmalloc \
--enable-gnuregex \
--disable-carp \
--enable-async-io=240 \
--with-pthreads \
--enable-storeio=ufs,aufs,diskd,null \
--disable-wccp \
--disable-wccpv2 \
--enable-kill-parent-hack \
--enable-cachemgr-hostname=localhost \
--enable-default-err-language=Simplify_Chinese \
--with-build-environment=POSIX_V6_ILP32_OFFBIG \
--with-maxfd=65535 \
--with-aio \
--disable-poll \
--enable-epoll \
--enable-linux-netfilter \
--enable-large-cache-files \
--disable-ident-lookups \
--enable-default-hostsfile=/etc/hosts \
--with-dl \
--with-large-files \
--enable-removal-policies=heap,lru \
--enable-delay-pools \
--enable-snmp \
--disable-internal-dns

        使用 centos 自带的 squid 已经满足需求,所以没必要编译安装。

        安装完成后,可以查看 squid 版本:

1
2
[root@192 ~]# squid -v
Squid Cache: Version 3.1.23

        同时还可以看到 squid 的编译参数。下面配置 squid,来实现正向代理:

1
2
[root@192 ~]# rm -f /etc/squid/squid.conf
[root@192 ~]# vim /etc/squid/squid.conf

        不实用默认的配置文件,删除以后重新写入以下配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
http_port 3128
acl manager proto cache_object
acl localhost src 127.0.0.1/32 ::1
acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1
acl localnet src 10.0.0.0/8 # RFC1918 possible internal network
acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl SSL_ports port 443
acl Safe_ports port 80 8080 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 # https
acl CONNECT method CONNECT
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow localnet
http_access allow localhost
http_access allow all
cache_dir aufs /data/cache 1024 16 256
cache_mem 128 MB
hierarchy_stoplist cgi-bin ?
coredump_dir /var/spool/squid
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
refresh_pattern \.(jpg|png|gif|mp3|xml) 1440 50% 2880 ignore-reload
refresh_pattern . 0 20% 4320

        配置文件中,第一行的 “http_port 3128” 这个指的是,squid 服务启动后将要监听的端口,也可以是 80 。“cache_dir” 这个用来指定本地磁盘上的缓存目录,后边的1024为大小,单位是 M 具体根据磁盘大小决定。“cache_mem”它用来规定缓存占用内存的大小,即把缓存的东西存到内存里,具体也需要根据机器的内存定,如果只跑 squid 服务,那么留给系统 512M 内存外,其他可以都分给 squid。

        配置文件保存后,可以先检测一下是否有语法错误:

1
2
[root@192 ~]# squid -kcheck
squid: ERROR: No running copy

        如果提示

1
squid: ERROR: No running copy

        这是 squid 还未启动,不过没有关系,显示成这样说明配置文件没有问题了。在启动前还得初始化缓存目录:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@192 ~]# mkdir /data/cache
[root@192 ~]# chown -R squid:squid /data/cache
[root@192 ~]# squid -z
2017/03/30 20:36:42| Creating Swap Directories
2017/03/30 20:36:42| /data/cache exists
2017/03/30 20:36:42| Making directories in /data/cache/00
2017/03/30 20:36:42| Making directories in /data/cache/01
2017/03/30 20:36:42| Making directories in /data/cache/02
2017/03/30 20:36:42| Making directories in /data/cache/03
2017/03/30 20:36:42| Making directories in /data/cache/04
2017/03/30 20:36:42| Making directories in /data/cache/05
2017/03/30 20:36:42| Making directories in /data/cache/06
2017/03/30 20:36:42| Making directories in /data/cache/07
2017/03/30 20:36:42| Making directories in /data/cache/08
2017/03/30 20:36:42| Making directories in /data/cache/09
2017/03/30 20:36:42| Making directories in /data/cache/0A
2017/03/30 20:36:42| Making directories in /data/cache/0B
2017/03/30 20:36:42| Making directories in /data/cache/0C
2017/03/30 20:36:42| Making directories in /data/cache/0D
2017/03/30 20:36:42| Making directories in /data/cache/0E
2017/03/30 20:36:42| Making directories in /data/cache/0F

        初始化完成后,就可以启动 squid 了:

1
2
[root@192 ~]# /etc/init.d/squid start
正在启动 squid:. [确定]

        查看 squid 是否启动:

1
[root@192 ~]# ps aux | grep squid

        现在可以在真机上测测看 squid 的正向代理了,需要设置 IE 选项,或者直接用 curl 命令测试:

1
[root@192 ~]# curl -xlocalhost:3128 http://www.baidu.com

        如果看到这样一大串,说明 squid 正向代理设置 OK 了,另外也可以观察 squid 对图片的缓存

1
[root@192 ~]# curl -xlocalhost:3128 http://www.aminglinux.com/bbs/static/image/common/logo.png -I

1
[root@192 ~]# curl -xlocalhost:3128 http://www.aminglinux.com/bbs/static/image/common/logo.png -I

        连续访问两次论坛的 logo 图片,可以发现前后两次的不同,其中“X-Cache-Lookup: HIT from 192.168.0.73:3128”显示,该请求已经 HIT ,它直接从本地的3128端口获取了数据。

        有时候还有这样的需求,就是想限制某些域名不能通过代理访问,或者说只想代理某几个域名,在 squid.conf 中找到:

1
acl CONNECT method CONNECT

        在其下面添加四行:

1
2
3
4
acl http proto HTTP
acl good_domain dstdomain .apelearn.com .aminglinux.com
http_access allow http good_domain
http_access deny http !good_domain

        其中白名单域名为“.apelearn.com .aminglinux.com”,这里的 . 表示万能匹配。前面可以是任何字符,只需要填写白名单域名即可。重启 squid 再来测测:

1
2
[root@192 ~]# /etc/init.d/squid restart
[root@192 ~]# curl -xlocalhost:3128 http://www.baidu.com/ -I

        访问百度已经变为403了,如果要设置黑名单,道理是一样的

1
2
3
4
acl http proto HTTP
acl bad_domain dstdomain .sina.com .souhu.com
http_access allow http !bad_domain
http_access deny http bad_domain

        重启 squid 后,测试:

1
2
3
[root@192 ~]# /etc/init.d/squid restart
[root@192 ~]# curl -xlocalhost:3128 http://www.sina.com/ -I
[root@192 ~]# curl -xlocalhost:3128 http://www.baidu.com/ -I

        baidu.com 可以访问,而 sina.com 不可以访问了